package com.salt.modules.sys.web;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.salt.common.json.AjaxJson;
import com.salt.common.utils.MyBeanUtils;
import com.salt.common.utils.StringUtils;
import com.salt.common.utils.sms.SmsUtils;
import com.salt.core.log.annotation.OperationRecord;
import com.salt.core.oss.OssTemplate;
import com.salt.core.oss.model.SaltFile;
import com.salt.core.web.BaseController;
import com.salt.modules.sys.entity.SysConfig;
import com.salt.modules.sys.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 系统配置Controller
 * @author salt
 * @version 2018-10-18
 */
@RestController
@RequestMapping("/sys/sysConfig")
public class SysConfigController extends BaseController {

	@Autowired
	private SysConfigService sysConfigService;
	@Autowired
	private OssTemplate ossTemplate;

	/*@Autowired
	private LoginService loginService;*/
	/**
	 * 系统配置列表页面
	 */
	@GetMapping("queryById")
	public AjaxJson queryById(HttpServletRequest request, HttpServletResponse response, Model model, String systemType) {

		//String systemType = loginService.getCachLoginSystemType(UserUtils.getUser().getId());
		SysConfig sysConfig = new SysConfig();
		sysConfig.setSystemType(systemType);
		SysConfig config = sysConfigService.getBySystemType(sysConfig);

		//SysConfig config = sysConfigService.get("1");
		if (config != null) {
			if (StringUtils.isNotBlank(config.getLogo())) {
				config.setLogo(ossTemplate.outerFileLink(config.getLogo()));
			}
			if (StringUtils.isNotBlank(config.getDefaultUserPhoto())) {
				config.setDefaultUserPhoto(ossTemplate.outerFileLink(config.getDefaultUserPhoto()));
			}
		}
		return AjaxJson.success().put("config", config);
	}

	@GetMapping("testSms")
	public AjaxJson testSms(@RequestParam("tel")String tel)throws ClientException {
		SendSmsResponse response =  SmsUtils.sendSms(tel,"{code:123}");
		if (response.getCode() != null && response.getCode().equals("OK")) {
			return AjaxJson.success("短信发送成功!");
		}else {
			return AjaxJson.error("短信发送失败!"+ response.getMessage());
		}
	}
	/**
	 * 保存系统配置
	 */
	@PostMapping("save")
	@OperationRecord(service = "系统服务", module = "系统配置", operation = "新增/删除", description = "")
	public AjaxJson save(SysConfig config)throws Exception {

		AjaxJson j = new AjaxJson();
		if(saltProperites.isDemoMode()){
			return AjaxJson.error("演示模式，禁止修改!");
		}
		if(config.getMultiAccountLogin() == null){
			config.setMultiAccountLogin("0");
		}

		//SysConfig target = sysConfigService.get("1");
		SysConfig target = sysConfigService.getBySystemType(config);
		if (target == null) {
			target = new SysConfig();
		}

		// 当配置了新的logo，删除文件服务器上原有的logo
		/*if (StringUtils.isNotBlank(config.getLogo()) && StringUtils.isNotBlank(target.getLogo())) {
			ossTemplate.removeFile(target.getLogo());
		}*/
		// 封装logo
		if (StringUtils.isNotBlank(config.getLogo())) {
			String[] split = config.getLogo().split("/", 5);
			config.setLogo(split[4]);
		}

		// 用户默认头像
		if (StringUtils.isNotBlank(config.getDefaultUserPhoto())) {
			String[] split = config.getDefaultUserPhoto().split("/", 5);
			config.setDefaultUserPhoto(split[4]);
		}

		MyBeanUtils.copyBeanNotNull2Bean(config, target);
		target.setIsNewRecord(false);
		sysConfigService.save(target);

		if (StringUtils.isNotBlank(target.getLogo())) {
			target.setLogo(ossTemplate.outerFileLink(target.getLogo()));
		}

		if (StringUtils.isNotBlank(target.getDefaultUserPhoto())) {
			target.setDefaultUserPhoto(ossTemplate.outerFileLink(target.getDefaultUserPhoto()));
		}

		return AjaxJson.success("保存系统配置成功").put("config", target);
	}

	@PostMapping("logoUpload")
	public AjaxJson logoUpload(HttpServletRequest request, HttpServletResponse response, MultipartFile file) throws IllegalStateException, IOException {
		AjaxJson j = new AjaxJson();
		// 判断文件是否为空
		if (!file.isEmpty()) {
			SaltFile saltFile = ossTemplate.putFile(file);
			j.put("url",saltFile.getOuterLink());
		}
		return j;
	}

}
